package com.edu.controller;

import com.edu.bean.User;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/*
 * @Author:princess
 * @Date 2023/11/9 11 09 9:58
 *
 */
@Controller
public class UserController {
    @Resource
    private UserService userServic;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Object userLogin(User user){
        ModelAndView mv = new ModelAndView();
        Map<String,Object> map = userServic.login(user);
        if (map.get("loginUser") != null){
            mv.setViewName("success.jsp");
            mv.addObject("loginUser",map.get("loginUser"));
        }else {
            mv.setViewName("login.jsp");
            mv.addObject("msg",map.get("msg"));
        }
        return mv;
    }
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Object userRegister(User user){
        ModelAndView mv = new ModelAndView();
        int result = userServic.register(user);
        if (result == 1){
            mv.setViewName("login.jsp");
        }else{
            mv.setViewName("register.jsp");
            mv.addObject("msg","用户名重复");
        }
        return mv;
    }
}
